package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Gift;
import com.ruoyi.market.service.IGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 礼物信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/gift")
public class GiftController extends BaseController
{
    @Autowired
    private IGiftService giftService;

    /**
     * 获取礼物列表
     */
    @PostMapping("/giftList")
    public TableDataInfo list(@RequestBody Gift gift, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            gift.setShopId(shopId);
        }
        startPageByPost();
        List<Gift> list = giftService.selectGiftList(gift);
        return getDataTable(list);
    }
    /**
     * 根据礼物编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:gift:query')")
    @GetMapping(value = { "/", "/{giftId}" })
    public AjaxResult getInfo(@PathVariable(value = "giftId", required = false) Long giftId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(giftId))
        {
            Gift sysGift = giftService.selectGiftById(giftId);
            ajax.put(AjaxResult.DATA_TAG, sysGift);
        }
        return ajax;
    }

    /**
     * 新增礼物
     */
    @PreAuthorize("@ss.hasPermi('market:gift:add')")
    @Log(title = "礼物管理 - 新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Gift gift, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            gift.setShopId(shopId);
        }
        if (!giftService.checkGiftUnique(gift))
        {
            return error("新增失败，已存在相同数据");
        }
        startPageByPost();
        gift.setCreateBy(getUsername());
        return toAjax(giftService.insertGift(gift));
    }

    /**
     * 修改礼物
     */
    @PreAuthorize("@ss.hasPermi('market:gift:edit')")
    @Log(title = "礼物管理 - 修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Gift gift)
    {
        if (!giftService.checkGiftUnique(gift))
        {
            return error("修改失败，已存在相同数据");
        }
        gift.setUpdateBy(getUsername());
        return toAjax(giftService.updateGift(gift));
    }

    /**
     * 删除礼物
     */
    @PreAuthorize("@ss.hasPermi('market:gift:remove')")
    @Log(title = "礼物管理 - 删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{giftIds}")
    public AjaxResult remove(@PathVariable Long[] giftIds)
    {
        return toAjax(giftService.deleteGiftByIds(giftIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:gift:edit')")
    @Log(title = "礼物管理 - 状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/giftChangeStatus")
    public AjaxResult changeStatus(@RequestBody Gift gift)
    {
        gift.setUpdateBy(getUsername());
        return toAjax(giftService.updateGiftStatus(gift));
    }
}
